from datetime import datetime
from applications.extensions import db
from flask import Blueprint, request, jsonify

from applications.extensions import db
from applications.models import ViolationInfo, BorrowInfo

violationInfo_bp = Blueprint('violationInfo', __name__, url_prefix='/violationInfo')

@violationInfo_bp.get('/getallViolationInfo')
def getallViolationInfo():
    try:
        telphonenum = request.args.get("telphonenum")
        borrow_info = BorrowInfo.query.filter_by(user_phonenum=telphonenum).all()
        borrow_info_list = []
        for info in borrow_info:
            if datetime.now() > info.should_return_time:
                violation_dict = {}
                violation_dict['book_id'] = info.book_id
                violation_dict['book_name'] = info.book_name
                violation_dict['isbn'] = info.isbn
                violation_dict['borrow_time'] = info.borrow_time.strftime('%Y-%m-%d %H:%M:%S')
                violation_dict['should_return_time'] = info.should_return_time.strftime('%Y-%m-%d %H:%M:%S')
                violation_dict['userborrow_status'] = info.status
                borrow_info_list.append(violation_dict)
        # violationInfo = ViolationInfo.query.filter_by(user_phonenum=telphonenum).all()
        # violationInfo_list = []
        # for info in violationInfo:
        #     violation_dict = {}
        #     violation_dict['id'] = info.id
        #     violation_dict['user_phonenum'] = info.user_phonenum
        #     violation_dict['violation_time'] = info.violation_time
        #     violation_dict['isbn'] = info.isbn
        #     violation_dict['violation_fee'] = info.violation_fee
        #     violationInfo_list.append(violation_dict)
        return jsonify(borrow_info_list)
    except BaseException as e:
        print(e)
        return {"code": 200, "message": "fail"}


@violationInfo_bp.get('/updateallViolationInfo')
def updateallViolationInfo():
    try:
        user_phonenum = request.args.get("telphonenum")
        borrow_info = BorrowInfo.query.filter_by(user_phonenum=user_phonenum).all()
        for info in borrow_info:
            if datetime.now() > info.should_return_time:
                time_diff = datetime.now() - info.should_return_time
                hours_diff = time_diff.total_seconds() / 3600.0
                newViolationInfo = ViolationInfo(user_phonenum=user_phonenum, violation_time=info.should_return_time, isbn=info.isbn,
                                                 violation_fee=hours_diff)
                db.session.add(newViolationInfo)
        db.session.commit()
        return {"code": 200, "message": "uptodate"}
    except BaseException as e:
        return {"code": 200, "message": "error"}


@violationInfo_bp.get('/getfeeViolationInfo')
def getfeeViolationInfo():
    try:
        user_phonenum = request.args.get("telphonenum")
        borrow_info = BorrowInfo.query.filter_by(user_phonenum=user_phonenum).all()
        for info in borrow_info:
            if datetime.now() > info.should_return_time:
                time_diff = datetime.now() - info.should_return_time
                hours_diff = time_diff.total_seconds() / 3600.0
                newViolationInfo = ViolationInfo(user_phonenum=user_phonenum, violation_time=info.should_return_time, isbn=info.isbn,
                                                 violation_fee=hours_diff)
                db.session.add(newViolationInfo)
        db.session.commit()
        return {"code": 200, "message": "uptodate"}
    except BaseException as e:
        return {"code": 200, "message": "error"}